package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.GoodsPrice;
import com.weipai.springboot.model.ShopCart;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.GoodsPriceDao;
import com.weipai.springboot.module.dao.ShopCartDao;
import com.weipai.springboot.module.mapper.ShopCartMapper;
import com.weipai.springboot.module.service.ShopCartService;
import com.weipai.springboot.module.util.SessionUserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/27 15:49
 */
@Service
public class ShopCartServiceImpl implements ShopCartService{

    private static final Logger log = LoggerFactory.getLogger(ShopCartServiceImpl.class);
    @Autowired
    ShopCartDao shopCartDao;
    @Autowired
    GoodsPriceDao goodsPriceDao;
    @Autowired
    SessionUserUtil sessionUserUtil;
    
    //加入购物车
    @Override
    @Transactional(readOnly=false, propagation = Propagation.REQUIRED)
    public Response addToShopCart(ShopCart shopCart) {
        Response response = new Response();
        String customerId = sessionUserUtil.getUserAttr().getCustomerId();
        shopCart.setCustomerId(customerId);
        if(shopCart.getGoodsNumber()==null || shopCart.getGoodsNumber()==0){
            shopCart.setGoodsNumber(1);
        }
        if(shopCartDao.vertifiData(customerId, shopCart.getGoodsId())==0){
            if(shopCartDao.addToShopCart(shopCart)==1){
                response.setCode(RetCode.SUCCESS);
            }else{
                log.info("加入购物车失败，用户id：{}"+customerId+",商品id：{}"+shopCart.getGoodsId());
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }else{
//            response.setCode(RetCode.HAVE_DATA);
            //查询出已存在的购物车信息
            ShopCart shopCart1 =shopCartDao.selectByCustomerIdAndGoodsId(customerId,shopCart.getGoodsId());
//            shopCart1.setGoodsNumber(shopCart1.getGoodsNumber()+1);
            shopCart1.setGoodsNumber(shopCart1.getGoodsNumber()+shopCart.getGoodsNumber());
            shopCartDao.updateByPrimaryKeySelective(shopCart1);
            response.setCode(RetCode.SUCCESS);
        }
        return response;
    }

    //购物车商品
    @Override
    public Response shopCartList() {
        Response response = new Response();
        String customerId = sessionUserUtil.getUserAttr().getCustomerId();
        String memberLevelId = sessionUserUtil.getUserAttr().getMemberLevelId();
        List<Map<String, Object>> map = shopCartDao.shopCartList(customerId);
        if(null!=map && map.size()>0){
            int size01 = map.size();
            for(int i=0;i<size01;i++){
                GoodsPrice price = goodsPriceDao.getGoodsPriceByGoodsIdAndMemberLevelId(map.get(i).get("goodsId").toString(), memberLevelId);
                if(null!=price){
                    map.get(i).put("price", price.getGoodsPrice());
                }else{
                    map.get(i).put("price", map.get(i).get("currentPrice"));
                }
                //增加信譽值


            }
            response.setCode(RetCode.SUCCESS);
            response.put("goodsList",map);
            response.put("useReputation",sessionUserUtil.getUserAttr().getUseReputation());
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //删除购物车商品
    @Override
    public Response deleteShopCartById(Integer[] ids) {
        Response response = new Response();
        if(ids.length>0){
            for (Integer id:ids) {
               int r = shopCartDao.deleteShopCartById(id);
               if(r>0){
                   response.setCode(RetCode.SUCCESS);
               }else {
                   response.setCode(RetCode.REQUEST_ERROR);
               }
            }
        }

        return response;
    }
}
